/**
 *
 */
package core;


import listener.OnDownloadListener;
import listener.OnGetListener;
import listener.OnPostListener;
import util.DownloadUtil;
import util.GetUtil;
import util.PostUtil;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * @author Crystalline
 * @GiteePage https://gitee.com/crystallineDW/krn
 */
public class KRN {

	private KRNmanager manager = null;

	private String url = "";

	private GetUtil util;


	public KRN() {
		util = new GetUtil();
	}

	/**
	 *
	 * @param manager Manager
	 * @param URL RequestAddress
	 * @param listener Responsible for listening
	 */
	//With parameter callbacks
	public  void get(KRNmanager manager, String URL, OnGetListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		GetUtil util = new GetUtil();
		util.Get_ChildThread(HttpClient, URL, listener);
	}

	//Direct callbacks
	public  void get(String URL,OnGetListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		GetUtil util = new GetUtil();
		util.Get_ChildThread(HttpClient, URL, listener);
	}


	//Return directly with parameters
	public String get(KRNmanager manager,String URL) {
		OkHttpClient HttpClient = managerBuild(manager);
		GetUtil util = new GetUtil();
		try {
			return util.Get_MainThread(HttpClient, URL);
		} catch (IOException e) {
			return e.toString();
		}
	}


	//Direct return
	public String get(String URL) {
		OkHttpClient HttpClient = new OkHttpClient();
		GetUtil util = new GetUtil();
		try {
			return util.Get_MainThread(HttpClient, URL);
		} catch (IOException e) {
			return e.toString();
		}
	}

	/**
	 *
	 * @param manager Manager
	 * @param URL RequestAddress
	 * @param postdata Post parameterspost parameters
	 * @param listener Responsible for listening
	 */

	//With parameter callbacks
	public  void post(KRNmanager manager, String URL, Map<String, String> postdata, OnPostListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		PostUtil postUtil = new PostUtil();
		postUtil.Post_ChildThread(HttpClient,URL,postdata,listener);
	}

	//Direct callbacks
	public  void post(String URL,Map<String, String> postdata,OnPostListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		PostUtil postUtil = new PostUtil();
		postUtil.Post_ChildThread(HttpClient,URL,postdata,listener);
	}

	//Return directly with parameters
	public String post(KRNmanager manager,String URL,Map<String, String> postdata) {
		OkHttpClient HttpClient = managerBuild(manager);
		PostUtil postUtil = new PostUtil();
		try {
			return postUtil.Post_MainThread(HttpClient,URL,postdata);
		} catch (Exception e) {
			return e.toString();
		}
	}

	//Direct return
	public String post(String URL,Map<String, String> postdata) {
		OkHttpClient HttpClient = new OkHttpClient();
		PostUtil postUtil = new PostUtil();
		try {
			return postUtil.Post_MainThread(HttpClient,URL,postdata);
		} catch (Exception e) {
			return e.toString();
		}
	}


	/**
	 *
	 * @param manager Manager
	 * @param URL DownloadAddress
	 * @param postdata Post parameterspost parameters
	 * @param SavePath FileSavePath
	 * @param listener Responsible for listening
	 */

	//管理器,重写
	public  void downloadByPost(KRNmanager manager, String URL, Map<String, String> postdata, String SavePath, String FileName, boolean IsOverWrite, OnDownloadListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadPOST(HttpClient,URL,postdata,SavePath,FileName,IsOverWrite,listener);
	}

	//管理器
	public  void downloadByPost(KRNmanager manager, String URL, Map<String, String> postdata, String SavePath, String FileName, OnDownloadListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		DownloadUtil downloadUtil = new DownloadUtil();

		downloadUtil.DownloadPOST(HttpClient,URL,postdata,SavePath,FileName,true,listener);
	}


	//重写
	public  void downloadByPost(String URL,Map<String, String> postdata,String SavePath,String FileName,boolean IsOverWrite,OnDownloadListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadPOST(HttpClient,URL,postdata,SavePath,FileName,IsOverWrite,listener);
	}

	//
	public  void downloadByPost(String URL,Map<String, String> postdata,String SavePath,String FileName,OnDownloadListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadPOST(HttpClient,URL,postdata,SavePath,FileName,true,listener);
	}

	//管理器,重写
	public  void downloadByGet(KRNmanager manager, String URL, String SavePath,String FileName,boolean IsOverWrite, OnDownloadListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadGET(HttpClient,URL,SavePath,FileName,IsOverWrite,listener);
	}

	//管理器
	public  void downloadByGet(KRNmanager manager, String URL, String SavePath,String FileName, OnDownloadListener listener) {
		OkHttpClient HttpClient = managerBuild(manager);
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadGET(HttpClient,URL,SavePath,FileName,true,listener);
	}

	//重写
	public  void downloadByGet(String URL,String SavePath,String FileName,boolean IsOverWrite,OnDownloadListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadGET(HttpClient,URL,SavePath,FileName,IsOverWrite,listener);
	}

	//
	public  void downloadByGet(String URL,String SavePath,String FileName,OnDownloadListener listener) {
		OkHttpClient HttpClient = new OkHttpClient();
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.DownloadGET(HttpClient,URL,SavePath,FileName,true,listener);
	}




	private OkHttpClient managerBuild(final KRNmanager manager) {
		//判断是否加入了请求头

		if(manager.Header!=null)
		{
			//加入了请求头
			manager.getBuilder().addInterceptor(new Interceptor() {

				@Override
				public Response intercept(Chain chain) throws IOException {
					Request.Builder requestbuilder = chain.request().newBuilder();
					for(Map.Entry<String, String> entry : manager.Header.entrySet()){
						String mapKey = entry.getKey();
						String mapValue = entry.getValue();

						requestbuilder.addHeader(mapKey, mapValue);
					}
					Request request  = requestbuilder.build();
					return chain.proceed(request);
				}
			});
		}
		return  manager.getBuilder().build();
	}
}
